#include <cxxtest/TestSuite.h>


#include "..\TinyCCMsg\TinyCCMsg\TinyTCPServer.h"
#include "..\TinyCCMsg\TinyCCMsg\TinyMsg.h"


#ifdef _WIN32
#   include <windows.h>
#   define CXXTEST_SAMPLE_GUI_WAIT() Sleep( 1000 )
#else // !_WIN32
    extern "C" unsigned sleep( unsigned seconds );
#   define CXXTEST_SAMPLE_GUI_WAIT() sleep( 1 )
#endif // _WIN32

class TinyMsgTestor: public CxxTest::TestSuite
{
public:
    void wait()
    {
        CXXTEST_SAMPLE_GUI_WAIT();
    }

    void test_Start_green()
    {
       // wait();
    }

    void test_Green_again()
    {
        //TS_TRACE( "Still green" );
       // wait();
    }

    void test_Now_yellow()
    {
        //TS_WARN( "Yellow" );
        //wait();
    }

    void test_Cannot_go_back()
    {
        //wait();
    }

    void test_Finally_red()
    {
       // TS_FAIL( "Red" );
        //wait();
    }

    void test_Cannot_go_back_to_yellow()
    {
       // TS_WARN( "Yellow?" );
        //wait();
    }

    void test_Cannot_go_back_to_green()
    {
        //wait();
    }
	void test_MsgHeaderandDefaultParam()
	{
		::TinyCC::msg::CTinyMsg msg;
		msg.setMsgItem(0x10);
		msg.setMsgIndex(0x20);
		TS_ASSERT_EQUALS( 0x10, msg.getMsgItem() );
		TS_ASSERT_EQUALS( 0x20, msg.getMsgIndex() );

		msg.pushBackDefaultParam("test0");
		std::string temp="test1";
		msg.pushBackDefaultParam(temp);
		msg.pushBackDefaultParam(std::string("test2"));


		TS_ASSERT_EQUALS(3,msg.getDefaultParamCount());
		std::string value;
		msg.getDefaultParamByIndex(1,value);
		TS_ASSERT_EQUALS("test1",value);
		msg.getDefaultParamByIndex(0,value);
		TS_ASSERT_EQUALS("test0",value);
		msg.getDefaultParamByIndex(2,value);
		TS_ASSERT_EQUALS("test2",value);
		wait();
	}

	void test_ExternalParam()
	{
		::TinyCC::msg::CTinyMsg msg;
		msg.setMsgItem(0x10);
		msg.setMsgIndex(0x20);
		msg.pushBackDefaultParam("test");
		std::string temp="test1";
		msg.pushBackDefaultParam(temp);
		msg.pushBackDefaultParam(std::string("test2"));

		msg.pushExternalParamByKey(0,"ExternalKey01","ExternalValue1");
		msg.pushExternalParamByKey(0,"ExternalKey01","ExternalValue2");
		msg.pushExternalParamByKey(1,"ExternalKey11","ExternalValue3");
		msg.pushExternalParamByKey(1,"ExternalKey12","ExternalValue4");
		msg.pushExternalParamByKey(2,"ExternalKey21","ExternalValue5");

		TS_ASSERT_EQUALS(1,msg.getExternalParamKeyCountByIndex(0));
		TS_ASSERT_EQUALS(2,msg.getExternalParamKeyCountByIndex(1));
		TS_ASSERT_EQUALS(1,msg.getExternalParamKeyCountByIndex(2));

		std::string keystr;
		
		msg._EnumnerateEPKeyByIndex(0,0,keystr);   //getkey by index
		TS_ASSERT_EQUALS("ExternalKey01",keystr);

		std::string temp2;
		msg.getExternalParamValueByEmunator(0,"ExternalKey01",0,temp2);
		TS_ASSERT_EQUALS("ExternalValue1",temp2);
		msg.getExternalParamValueByEmunator(1,"ExternalKey11",0,temp2);
		TS_ASSERT_EQUALS("ExternalValue3",temp2);
		msg.getExternalParamValueByEmunator(2,"ExternalKey21",0,temp2);
		TS_ASSERT_EQUALS("ExternalValue5",temp2);

		wait();
	}
	void test_msgSerialize()
	{
		::TinyCC::msg::CTinyMsg msg;
		msg.setMsgItem(0x10);
		msg.setMsgIndex(0x20);
		msg.pushBackDefaultParam("test");
		std::string temp="test1";
		msg.pushBackDefaultParam(temp);
		msg.pushBackDefaultParam(std::string("test2"));

		msg.pushExternalParamByKey(0,"ExternalKey01","ExternalValue1");
		msg.pushExternalParamByKey(0,"ExternalKey01","ExternalValue2");
		msg.pushExternalParamByKey(1,"ExternalKey11","ExternalValue3");
		msg.pushExternalParamByKey(1,"ExternalKey12","ExternalValue4");
		msg.pushExternalParamByKey(2,"ExternalKey21","ExternalValue5");

		long length=0;

		const char * test=msg.serialize(length);
		TS_ASSERT_LESS_THAN(0,length);

		::TinyCC::msg::CTinyMsg msg2;
		msg2.clear();
		TS_ASSERT_EQUALS(0,msg2.getDefaultParamCount());
		msg2.deSerialize(test,length);
		TS_ASSERT_EQUALS(3,msg2.getDefaultParamCount());
		TS_ASSERT_EQUALS(3,msg2.getExternalParamMapSize());

		TS_ASSERT_EQUALS(1,msg2.getExternalParamKeyCountByIndex(0));
		TS_ASSERT_EQUALS(2,msg2.getExternalParamKeyCountByIndex(1));
		TS_ASSERT_EQUALS(1,msg2.getExternalParamKeyCountByIndex(2));

		std::string keystr;
		
		msg2._EnumnerateEPKeyByIndex(0,0,keystr);   //getkey by index
		TS_ASSERT_EQUALS("ExternalKey01",keystr);

		std::string temp2;
		msg2.getExternalParamValueByEmunator(0,"ExternalKey01",0,temp2);
		TS_ASSERT_EQUALS("ExternalValue1",temp2);
		msg2.getExternalParamValueByEmunator(1,"ExternalKey11",0,temp2);
		TS_ASSERT_EQUALS("ExternalValue3",temp2);
		msg2.getExternalParamValueByEmunator(2,"ExternalKey21",0,temp2);
		TS_ASSERT_EQUALS("ExternalValue5",temp2);


		wait();

	}
};
